import { Router } from 'express';
import dayjs from 'dayjs';
import Sku from '../model/sku.js';
import auth from '../utils/auth.js';

const router = Router();

router.use(auth);

router.use(function timeLog(req, res, next) {
  console.log('api request time', dayjs().format('YYYY-MM-DD HH:mm:ss'));
  next();
});

router.get('/', async function (req, res) {
  let { pageNum, pageSize } = req.query;

  let list = await Sku.query().forPage(pageNum, pageSize).get();
  if (pageNum == 1) {
    let count = await Sku.query().count();
    res.send({ list, count });
  } else {
    res.send({ list });
  }
});

router.post('/', async (req, res) => {
  let form = req.body;
  console.log('form', form);
  return form;
});

export default router;
